package com.lkd.service.impl;


import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lkd.entity.*;
import com.lkd.mapper.*;
import com.lkd.service.NodeService;
import com.lkd.service.RegionService;
import com.lkd.service.VendingMachineService;
import com.lkd.vo.NodeVo;
import com.lkd.vo.PageBean;
import com.lkd.vo.RegionVo;
import com.lkd.vo.VendingMachineVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 点位表 服务实现类
 * </p>
 *
 * @author 聂德祥
 * @since 2024-08-19
 */
@Service
public class NodeServiceImpl extends ServiceImpl<NodeMapper, Node> implements NodeService {
@Autowired
private RegionMapper regionMapper;
@Autowired
private VendingMachineMapper vendingMachineMapper;
@Autowired
private BusinessMapper businessMapper;
@Autowired
private VmTypeMapper vmTypeMapper;
    @Override
    public PageBean<NodeVo> search(Integer regionId,String name, Integer pageIndex, Integer pageSize) {
        Page<Node> nodePage = new Page<>(pageIndex, pageSize);
        Page<NodeVo> nodeVoPage = new Page<>(pageIndex, pageSize);
        LambdaQueryWrapper<Node> queryWrapper = new LambdaQueryWrapper<Node>().eq(regionId != null, Node::getRegionId, regionId).like(StrUtil.isNotEmpty(name), Node::getName, name);
        nodePage= getBaseMapper().selectPage(nodePage, queryWrapper);
        if (nodePage.getRecords().size() > 0) {
            nodeVoPage.setTotal(nodePage.getTotal());
            nodeVoPage.setSize(nodePage.getSize());
            nodeVoPage.setCurrent(nodePage.getCurrent());
            List<NodeVo> nodeVos = nodePage.getRecords().stream().map(node -> {
                NodeVo nodeVo = BeanUtil.copyProperties(node, NodeVo.class);

                // 获取区域
                RegionVo regionVo = regionMapper.getRegionVo(node.getRegionId());
                if (regionVo != null) {
                    nodeVo.setRegion(regionVo);
                }
                // 获取商圈类型
                Business business = businessMapper.selectById(node.getBusinessId());
                if (business != null) {
                    nodeVo.setBusinessType(business);
                }
                // 获取设备数量
                Long aLong = vendingMachineMapper.selectCount(new LambdaQueryWrapper<VendingMachine>().eq(VendingMachine::getNodeId, node.getId()));
                nodeVo.setVmCount(aLong.intValue());
                return nodeVo;
            }).collect(Collectors.toList());
            if(CollUtil.isNotEmpty(nodeVos)){
                nodeVoPage.setRecords(nodeVos);
            }
        }
        return new PageBean<NodeVo>(nodeVoPage);
    }

    @Override
    public NodeVo findById(Long nodeId) {
        Node node = getBaseMapper().selectById(nodeId);
        if(node!=null){

            NodeVo nodeVo = BeanUtil.copyProperties(node, NodeVo.class);
            RegionVo regionVo = regionMapper.getRegionVo(node.getRegionId());
            if (regionVo != null) {
                nodeVo.setRegion(regionVo);
            }
            // 获取商圈类型
            Business business = businessMapper.selectById(node.getBusinessId());
            if (business != null) {
                nodeVo.setBusinessType(business);
            }
            // 获取设备数量
            Long aLong = vendingMachineMapper.selectCount(new LambdaQueryWrapper<VendingMachine>().eq(VendingMachine::getNodeId, node.getId()));
            nodeVo.setVmCount(aLong.intValue());
            return nodeVo;
        }
        return null;
    }

    @Override
    public List<VendingMachineVo> findVMById(Long nodeId) {
        LambdaQueryWrapper<VendingMachine> eq = new LambdaQueryWrapper<VendingMachine>().eq(VendingMachine::getNodeId, nodeId);
        List<VendingMachine> vendingMachines = vendingMachineMapper.selectList(eq);
        List<VendingMachineVo> vendingMachineVos;
        if(CollUtil.isNotEmpty(vendingMachines)){
            vendingMachineVos = vendingMachines.stream().map(vendingMachine -> {

                VendingMachineVo vendingMachineVo = BeanUtil.copyProperties(vendingMachine, VendingMachineVo.class);
                VmType vmType = vmTypeMapper.selectById(vendingMachine.getVmType());
                if(vmType!=null){
                    vendingMachineVo.setType(vmType);
                }
                NodeVo nodeVo = findById(vendingMachine.getNodeId());

                if(nodeVo!=null){
                    vendingMachineVo.setNode(nodeVo);
                    if(nodeVo.getRegion()!=null){
                        vendingMachineVo.setRegion(nodeVo.getRegion());
                    }
                }

                return vendingMachineVo;
            }).collect(Collectors.toList());
            return vendingMachineVos;
        }
        return null;
    }

    @Override
    public Integer getCount() {
        return baseMapper.getCount();
    }
}
